package com.help10000.rms.mvp.presenter

import com.amir.common.api.CustomObserver
import com.amir.common.beans.Cmpy
import com.amir.common.beans.CompanyCatgListEntity
import com.amir.common.beans.CompanyLoginEntity
import com.amir.common.mvp.model.Model
import com.amir.common.utils.Constant
import com.amir.common.utils.SPUtils
import com.help10000.rms.config.globalData
import com.help10000.rms.mvp.view.SelectCompanyView

class SelectCompanyPresenter(view: SelectCompanyView) : CompanyListPresenter(view) {
    fun companyLogin(cmpy: Cmpy) {
        var AccessToken = SPUtils.getString(Constant.AccessToken)
        val accountid = SPUtils.getString(Constant.accountid)
        val companyLogin =
            Model.getServer().companyLogin(AccessToken, "6666", accountid, cmpy.CmpyCode)
        Model.getObservable(companyLogin, object : CustomObserver<CompanyLoginEntity>(mvpView) {
            override fun success(t: CompanyLoginEntity) {
                t.data.save()
                globalData.Cmpy = cmpy
                mvpView?.companyLoginSuccess()
                companyCatgList(t.data.CmpyCatg, AccessToken)
            }

        })
    }

    fun companyCatgList(cmpyCatg:Int, accessToken: String?){
        val result = Model.getServer().companyCatgList(accessToken)
        Model.getObservable(result, object : CustomObserver<CompanyCatgListEntity>(mvpView){
            override fun success(t: CompanyCatgListEntity) {
                val list = t.data.CompanyCatgInfos
                for (cc in list){
                    if(cc.ID == cmpyCatg){
                        globalData.CmpyCatg = cc
                        break
                    }
                }
            }
        })
    }

    override fun getMvpView(): SelectCompanyView? {
        return super.getMvpView() as SelectCompanyView?
    }


}